﻿using ContainerModule.ServiceReference1;
using Microsoft.Practices.Prism.Commands;
using System;
using System.Collections.Generic;
using System.Data.Services.Client;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace ContainerModule.ViewModels
{
    public class ContainerViewModel
    {
        ServiceReference1.Container context = new ServiceReference1.Container(new Uri("http://localhost:11245/odata"));

        public ContainerViewModel()
        {
            this.InsertCommand = new DelegateCommand<container>(this.OnInsert);
            

        }

        public ICommand InsertCommand { get; private set; }
        public ICommand DeleteCommand { get; private set; }
        public ICommand UpdateCommand { get; private set; }





        private void OnInsert(container obj)
        {

            var Con2 = new container
            {
              container_decription = obj.container_decription,
              container_name = obj.container_name
            };

            context.AddTocontainers(Con2);

            context.SaveChanges();

            MessageBox.Show(Con2.container_name + " 가 입력 되었습니다.", "입력완료 메세지");

        }

        private void OnDelete(container obj)
        {
            var delete = obj.container_name;

            //MessageBox.Show(delete);
            container selectItem = context.containers
                                        .Where(co => co.container_name == delete)
                                        .SingleOrDefault<container>();


                context.DeleteObject(selectItem);
                context.SaveChanges();

            MessageBox.Show(obj.container_name + " 가 삭제 되었습니다.", "삭제완료 메세지");

        }

        private void OnUpdate(container obj)
        {
            var update = obj.container_name;
          

            //MessageBox.Show(delete);
            container selectItem = context.containers
                                        .Where(con => con.container_name == update)
                                        .SingleOrDefault<container>();

            selectItem.container_decription = obj.container_decription;

            context.UpdateObject(selectItem);
            context.SaveChanges();

            MessageBox.Show(obj.container_name + " 가 변경 되었습니다.", "수정완료 메세지");

        }

    }

}
